package com.ytkj.digitalConstruction.controller.project;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ytkj.digitalConstruction.ReturnCode;
import com.ytkj.digitalConstruction.entity.dept.Dept;
import com.ytkj.digitalConstruction.entity.project.Project;
import com.ytkj.digitalConstruction.httpbean.project.ResProject;
import com.ytkj.digitalConstruction.service.project.ProjectService;
import com.ytkj.digitalConstruction.util.WebTokenUtil;

/**
 * 项目请求入口
 * 
 * @author Pete
 * @Date 2017年12月7日
 */
@RestController
@RequestMapping("/projects")
public class ProjectController {

	private Logger log = LoggerFactory.getLogger(getClass());

	@Autowired
	private ProjectService projectService;

	/**
	 * 新增一个项目
	 * 
	 * @return
	 */
	@PostMapping("/add")
	public ReturnCode add(@RequestBody @Valid Project project, HttpServletRequest request, BindingResult bindingResult) {
		// 判断参数是否合法
		if (bindingResult.hasErrors()) {
			String msg = bindingResult.getFieldError().getDefaultMessage();
			log.error(msg);
			return new ReturnCode.Builder().Success(false).Msg(msg).build();
		}
		//检验合同编号
		if(project.getContractNumber().equals("")||project.getContractNumber() == null ||project.getContractNumber().length()==0) {
			return new ReturnCode.Builder().Success(false).Msg("合同编号不能为空，请重新填写！").build();
		}
		// 校验部门
		Dept dept = projectService.selectByDeptId(project.getDeptId());
		if (dept.getDeptType() == 1) {
			return new ReturnCode.Builder().Success(false).Msg("项目所属部门为虚拟部门，请选择虚拟部门").build();
		}
		Dept constructDept = projectService.selectByDeptId(project.getConstructDeptId());
		if (constructDept.getDeptType() == 2) {
			return new ReturnCode.Builder().Success(false).Msg("施工部门为实体部门，请选择实体部门").build();
		}
		Dept detectionDept = projectService.selectByDeptId(project.getDetectionDeptId());
		if (detectionDept.getDeptType() == 2) {
			return new ReturnCode.Builder().Success(false).Msg("监理部门为实体部门，请选择实体部门").build();
		}
		Dept supervisorDept = projectService.selectByDeptId(project.getSupervisorDeptId());
		if (supervisorDept.getDeptType() == 2) {
			return new ReturnCode.Builder().Success(false).Msg("检测部门为实体部门，请选择实体部门").build();
		}
		// 添加项目
		boolean result = projectService.addProject(project, request);
		if (result == false) {
			log.error("添加项目失败");
			return new ReturnCode.Builder().Success(false).Msg("添加下项目失败!").build();
		}
		log.info("添加项目成功");
		return new ReturnCode.Builder().Success(true).Msg("添加项目成功!").Object(project).build();
	}

	/**
	 * 修改项目
	 * 
	 * @param project
	 * @return
	 */
	@PostMapping("/update")
	public ReturnCode update(@RequestBody @Valid Project project, BindingResult bindingResult) {
		// 判断参数是否合法
		if (bindingResult.hasErrors()) {
			String msg = bindingResult.getFieldError().getDefaultMessage();
			log.error(msg);
			return new ReturnCode.Builder().Success(false).Msg(msg).build();
		}
		Dept dept = projectService.selectByDeptId(project.getDeptId());
		if (dept.getDeptType() == 1) {
			return new ReturnCode.Builder().Success(false).Msg("项目所属部门为虚拟部门，请选择虚拟部门").build();
		}
		Dept constructDept = projectService.selectByDeptId(project.getConstructDeptId());
		if (constructDept.getDeptType() == 2) {
			return new ReturnCode.Builder().Success(false).Msg("施工部门为实体部门，请选择实体部门").build();
		}
		Dept detectionDept = projectService.selectByDeptId(project.getDetectionDeptId());
		if (detectionDept.getDeptType() == 2) {
			return new ReturnCode.Builder().Success(false).Msg("监理部门为实体部门，请选择实体部门").build();
		}
		Dept supervisorDept = projectService.selectByDeptId(project.getSupervisorDeptId());
		if (supervisorDept.getDeptType() == 2) {
			return new ReturnCode.Builder().Success(false).Msg("检测部门为实体部门，请选择实体部门").build();
		}
		boolean result = projectService.updateProject(project);
		if (result == false) {
			log.error("修改项目失败");
			return new ReturnCode.Builder().Success(false).Msg("修改项目失败").build();
		}
		log.info("修改项目成功");
		return new ReturnCode.Builder().Success(true).Msg("修改项目成功").Object(project).build();
	}

	/**
	 * 查询用户的所有项目
	 * 
	 * @param userId
	 * @return
	 */
	@GetMapping("/gets")
	public ReturnCode gets(HttpServletRequest request) {
		// 从request中获取用户Id
		Integer userId = WebTokenUtil.getUserId(request);
		// 开始查询用户的所有项目
		List<ResProject> list = projectService.getProjects(userId);
		list.sort((ResProject h1, ResProject h2) -> h2.getProjectId().compareTo(h1.getProjectId()));
		log.info("查询用户的所有项目成功");
		return new ReturnCode.Builder().Success(true).Msg("查询用户的所有项目成功").Object(list).build();
	}
}
